package azkaban.webapp.plugin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class ConfigurationManager {
	private static final Log logger = LogFactory.getLog(ConfigurationManager.class);
	private static Properties prop = new Properties();

	static {
		try {
			InputStream in = ConfigurationManager.class.getClassLoader().getResourceAsStream("conf/azkaban.properties");
			prop.load(new InputStreamReader(in, "UTF-8"));  
		}catch(Exception e) {
			logger.error("config.properties文件读取与解析异常",e);
		}		
	}

	private ConfigurationManager() {
	}

	/**
	 * 获取指定key对应的value
	 * @param key 
	 * @return value
	 */
	public static String getProperty(String key) {
		return prop.getProperty(key);
	}
	
	/**
	 * 获取整数类型的配置项
	 * @param key
	 * @return value
	 */
	public static Integer getInteger(String key) {
		String value = getProperty(key);
		try {
			return Integer.valueOf(value);
		} catch (Exception e) {
			logger.error("获取异常",e);
		}
		return 0;
	}
	
	/**
	 * 获取布尔类型的配置项
	 * @param key
	 * @return value
	 */
	public static Boolean getBoolean(String key) {
		String value = getProperty(key);
		try {
			return Boolean.valueOf(value);
		} catch (Exception e) {
			logger.error("获取异常",e);
		}
		return false;
	}
	
	/**
	 * 获取Long类型的配置项
	 * @param key
	 * @return
	 */
	public static Long getLong(String key) {
		String value = getProperty(key);
		try {
			return Long.valueOf(value);
		} catch (Exception e) {
			logger.error("获取异常",e);
		}
		return 0L;
	}
}
